package net.neptech.controller;

import lombok.RequiredArgsConstructor;
import net.neptech.bo.MenuBo;
import net.neptech.model.sys.Menu;
import net.neptech.service.MenuService;
import net.neptech.utils.BeanCopyUtils;
import net.neptech.utils.SecurityUtils;
import net.neptech.vm.ResponseResult;
import net.neptech.vo.MenuVo;
import net.neptech.vo.RouterVo;
import net.neptech.vo.TreeSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: liyuxiang
 * @Date: 2025/8/13 18:27
 * @Description: TODO
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public ResponseResult<List<RouterVo>> getRouters() {
        List<Menu> menus = menuService.selectMenuTreeByUserId(SecurityUtils.getUserId());
        return ResponseResult.success(menuService.buildMenus(menus));
    }

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public ResponseResult<List<MenuVo>> list(MenuBo menuBo) {
        List<MenuVo> menus = menuService.selectMenuList(menuBo, SecurityUtils.getUserId());
        return ResponseResult.success(menus);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeSelect")
    public ResponseResult<List<TreeSelectVo>> treeSelect(MenuBo menu) {
        List<MenuVo> menus = menuService.selectMenuList(menu, SecurityUtils.getUserId());
        return ResponseResult.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 新增菜单
     */
    @PostMapping("")
    public ResponseResult<MenuVo> add(@RequestBody MenuBo menuBo) {
        menuService.updateMenu(menuBo);
        return ResponseResult.success();
    }

    /**
     * 更新菜单
     */
    @PutMapping("")
    public ResponseResult<MenuVo> update(@RequestBody MenuBo menuBo) {
        menuService.updateMenu(menuBo);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("{menuId}")
    public ResponseResult<MenuVo> deleteMenuById(@PathVariable("menuId") Long menuId) {
        menuService.deleteMenuById(menuId);
        return ResponseResult.success();
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeSelect/{roleId}")
    public ResponseResult<?> roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        //先获取当前用户的菜单
        List<Menu> menus = menuService.selectMenuList(SecurityUtils.getUserId());

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("menus", menuService.buildMenuTreeSelect(BeanCopyUtils.copyBeanList(menus, MenuVo.class)));
        //获取当前角色被分配的菜单
        hashMap.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        return ResponseResult.success(hashMap);
    }

    @GetMapping("{menuId}")
    public ResponseResult<MenuVo> getMenuById(@PathVariable("menuId") Long menuId) {
        MenuVo menuVo = menuService.selectMenuById(menuId);
        return ResponseResult.success(menuVo);
    }

    /**
     * test
     */
    @GetMapping("/test")
    public ResponseResult<String> test() {
        return ResponseResult.success();
    }
}
